package com.caoyuqi.Service;

import com.caoyuqi.dao.GoodsDao;
import com.caoyuqi.dao.SaleDao;
import com.caoyuqi.domain.Goods;
import com.caoyuqi.utils.NoEnoughException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class Buy {


    @Autowired
    GoodsDao goodsDao;
    @Autowired
    SaleDao saleDao;
    @Transactional(
            rollbackFor = NoEnoughException.class
    )
    public void buyGoods(Integer goodsId,Integer nums)
    {
        //插入新的销售记录
        saleDao.InsertIntoSale(goodsId,nums);
        //查询相关商品
        Goods goods = goodsDao.SelectGoodByGid(goodsId);
        if(goods==null)
        {
            throw new NullPointerException("没有该商品");
        }else if(goods.getAmount()<nums)
        {
            throw new NoEnoughException("商品数量不足异常");
        }
        goodsDao.UpdateGoods(new Goods(goodsId,3,nums,"牛奶"));
    }
}
